package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrateCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatsResult;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model){
        //commends5条攻略推荐
        model.addAttribute("commends",strategyCommendService.listTop5());
        //upCds,飙升排行榜
        model.addAttribute("upCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_UP_SORT));
        //hotCds,热门排行榜
        model.addAttribute("hotCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_HOT_SORT));
        //unabroads  国内
        List<StatsResult> unabroads = strategySearchService.listCondition(SearchQueryObject.CONDITION_UNABROAD);

        model.addAttribute("unabroads",unabroads);
        //abroads  国外
        List<StatsResult> abroads = strategySearchService.listCondition(SearchQueryObject.CONDITION_ABROAD);
        abroads.remove(StatsResult.CHINA_ID);
        model.addAttribute("abroads",abroads);
        //themes  主题攻略
        List<StatsResult> themes = strategySearchService.listCondition(SearchQueryObject.CONDITION_THEME);
        model.addAttribute("themes",themes);

        //themeCds 主题推荐
        model.addAttribute("themeCds",strategySearchService.listThemeTop10());
        return "strategy/index";
    }


    @RequestMapping("detail")
    public String listDetail(Long id,@LoginUser UserInfo user, Model model){

        //阅读量加1
        strategyStatsCacheService.incrNum(id,1,IStrategyStatsCacheService.STRATEGY_STATS_VIEWNUM);
        StrategyDetail detail = strategyDetailService.get(id);
        model.addAttribute("detail",detail);
        model.addAttribute("vo",strategyStatsCacheService.getStrategyStats(id));
        //isFavor是否收藏的回显
        boolean isFavor = true;
        if (user == null){
            isFavor = false;
        }else {
            isFavor = strategyStatsCacheService.isFavor(id,user.getId());
        }
        model.addAttribute("isFavor",isFavor);
        //飙升榜加分
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,1,id);
        return "strategy/detail";
    }



    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;

    /**
     * 查看所有游记攻略
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyQueryObject qo,Model model){

        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //tags
        List<StrategyTag> tags = strategyTagService.selectByDestId(qo.getDestId());

        model.addAttribute("tags",tags);
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",toasts.remove(toasts.size()-1));

        return "strategy/list";
    }

    @Reference
    private IStrategyCommentService strategyCommentService;


    @RequestMapping("comment")
    public String comment(@ModelAttribute("qo")StrateCommentQueryObject qo, Model model){
        Page<StrategyComment> page = strategyCommentService.findByDetailId(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }



    /**
     * 添加评论
     * @return
     */
    @RequireLogin
    @RequestMapping("commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        //填充用户信息
        comment.setUserId(user.getId());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setUsername(user.getNickname());
        comment.setLevel(user.getLevel());
        //保存评论
        strategyCommentService.save(comment);
        //评论数加一
        strategyStatsCacheService.incrNum(comment.getDetailId(),1,IStrategyStatsCacheService.STRATEGY_STATS_REPLYNUM);
        //查询评论数
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(comment.getDetailId());
        //飙升榜加分
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,1,comment.getDetailId());
        return result.addData(vo.getReplynum());
    }


    /**
     * 点赞/取消点赞
     * @param toId
     * @param fromId
     * @return
     */
    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toId,Long fromId){
        strategyCommentService.commentThumbUp(toId,fromId);
        return new AjaxResult();
    }

    /**
     * 点击收藏
     * @param strategyId
     * @return
     */
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long strategyId,@LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        //判断是否有登录,只有登录了才能点收藏
        if (user == null){
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        boolean flag = strategyStatsCacheService.favor(strategyId, user.getId());
        //热门榜加分
        int num = flag ? 1 : -1;
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,num,strategyId);
        result.setSuccess(flag);
        //查询攻略的统计数
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(strategyId);
        return result.addData(vo);
    }

    /**
     * 文章攻略的点赞
     * @return
     */
    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long strategyId,@LoginUser UserInfo user){
        AjaxResult result = new AjaxResult();
        //判断用户是否已经登陆
        if (user == null){
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        //
        boolean flag = strategyStatsCacheService.strategyThumbup(strategyId, user.getId());
        //热门榜加分
        int num = flag ? 1 : -1;
        strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,num,strategyId);
        result.setSuccess(flag);
        //查询点赞数
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(strategyId);
        return result.addData(vo);
    }

    @RequestMapping("searchPage")
    public String searchPage(@ModelAttribute("qo")SearchQueryObject qo,Model model){
        //page
        model.addAttribute("page",strategySearchService.query(qo));
        return "strategy/searchPageTpl";
    }

    @RequestMapping("share")
    @ResponseBody
    public Object share(Long strategyId){
        AjaxResult result = new AjaxResult();
        strategyStatsCacheService.incrNum(strategyId,1,IStrategyStatsCacheService.STRATEGY_STATS_SHARENUM);
        return result;
    }

}
